import { _decorator, AudioClip, AudioSource, Component, director, Node, ProgressBar, resources } from 'cc';
import { PlayerControl } from './PlayerControl';
const { ccclass, property } = _decorator;

@ccclass('GameManager')
export class GameManager extends Component {

  audio: AudioSource = null
  flashSound: AudioClip = null
  runSound: AudioClip = null
  kattackSound: AudioClip = null
  hpCount: number = 1
  mpCount: number = 1
  hp: ProgressBar = null
  mp: ProgressBar = null

  start() {
    this.hp = this.node.getParent().getChildByName('Camera').getChildByName('status').getChildByName('hp').getComponent(ProgressBar)
    this.mp = this.node.getParent().getChildByName('Camera').getChildByName('status').getChildByName('mp').getComponent(ProgressBar)
    this.audio = this.getComponent(AudioSource)
    resources.load('flashSound', AudioClip, (e, clip) => {
      this.flashSound = clip
    })
    resources.load('runSound', AudioClip, (e, clip) => {
      this.runSound = clip
    })
    resources.load('kattackSound', AudioClip, (e, clip) => {
      this.kattackSound = clip
    })
  }

  update(deltaTime: number) {
    this.changeHP(this.hpCount, deltaTime)
  }
  flashVoice() {
    this.audio.playOneShot(this.flashSound, 1)
  }
  kattackVoice() {
    this.audio.playOneShot(this.kattackSound, 1)
  }
  runVoice(isPlay: boolean) {
    if (!isPlay) {
      this.audio.pause()
      return
    }
    this.audio.clip = this.runSound
    this.audio.loop = true
    this.audio.play()
  }
  shop() {
    director.loadScene('shop')
  }
  home() {
    director.loadScene('home')
  }
  main(){
    director.loadScene('scene')
  }
  changeHP(count: number, dt: number) {
    if(this.hpCount<=0.2){
      this.node.getParent().getChildByName('player').getComponent(PlayerControl).die()
      director.loadScene('gameover')

    }
    if (this.hp.progress > count) {
      this.hp.progress -= 0.5 * dt
    }
  }
}


